using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
using 路运输从业人员无纸化考试系统;
using 道路运输从业人员无纸化考试系统.Properties;

namespace 道路运输从业人员无纸化考试系统
{
    public class FirstForm : Form
    {
        private IContainer components = null;

        private Button btnSJ;

        private Button btnMN;

        private Button btnZJ;

        private Label lblKF;

        private Label lblJZ;

        private Label lblName;
        private Timer timer1;
        private DateTime endTime = Convert.ToDateTime("2021-10-1");
        private string title = "湖北省道路运输从业人员无纸化考试系统";
        private bool IsUse = false;
        private string errorMsg = "";
        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FirstForm));
            this.btnSJ = new System.Windows.Forms.Button();
            this.btnMN = new System.Windows.Forms.Button();
            this.btnZJ = new System.Windows.Forms.Button();
            this.lblKF = new System.Windows.Forms.Label();
            this.lblJZ = new System.Windows.Forms.Label();
            this.lblName = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.SuspendLayout();
            // 
            // btnSJ
            // 
            this.btnSJ.BackColor = System.Drawing.Color.Transparent;
            this.btnSJ.BackgroundImage = global::道路运输从业人员无纸化考试系统.Properties.Resources._22;
            this.btnSJ.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.btnSJ.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnSJ.Location = new System.Drawing.Point(405, 342);
            this.btnSJ.Name = "btnSJ";
            this.btnSJ.Size = new System.Drawing.Size(203, 207);
            this.btnSJ.TabIndex = 3;
            this.btnSJ.UseVisualStyleBackColor = false;
            this.btnSJ.Click += new System.EventHandler(this.btnSJ_Click);
            this.btnSJ.MouseEnter += new System.EventHandler(this.btnSJ_MouseEnter);
            this.btnSJ.MouseLeave += new System.EventHandler(this.btnSJ_MouseLeave);
            // 
            // btnMN
            // 
            this.btnMN.BackColor = System.Drawing.Color.Transparent;
            this.btnMN.BackgroundImage = global::道路运输从业人员无纸化考试系统.Properties.Resources._33;
            this.btnMN.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.btnMN.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnMN.Location = new System.Drawing.Point(625, 342);
            this.btnMN.Name = "btnMN";
            this.btnMN.Size = new System.Drawing.Size(203, 207);
            this.btnMN.TabIndex = 4;
            this.btnMN.UseVisualStyleBackColor = false;
            this.btnMN.Click += new System.EventHandler(this.btnMN_Click);
            this.btnMN.MouseEnter += new System.EventHandler(this.btnMN_MouseEnter);
            this.btnMN.MouseLeave += new System.EventHandler(this.btnMN_MouseLeave);
            // 
            // btnZJ
            // 
            this.btnZJ.BackColor = System.Drawing.Color.Transparent;
            this.btnZJ.BackgroundImage = global::道路运输从业人员无纸化考试系统.Properties.Resources._11;
            this.btnZJ.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.btnZJ.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnZJ.Location = new System.Drawing.Point(186, 342);
            this.btnZJ.Name = "btnZJ";
            this.btnZJ.Size = new System.Drawing.Size(203, 207);
            this.btnZJ.TabIndex = 2;
            this.btnZJ.UseVisualStyleBackColor = false;
            this.btnZJ.Click += new System.EventHandler(this.btnZJ_Click);
            this.btnZJ.MouseEnter += new System.EventHandler(this.btnZJ_MouseEnter);
            this.btnZJ.MouseLeave += new System.EventHandler(this.btnZJ_MouseLeave);
            // 
            // lblKF
            // 
            this.lblKF.AutoSize = true;
            this.lblKF.BackColor = System.Drawing.Color.Transparent;
            this.lblKF.Font = new System.Drawing.Font("华文宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblKF.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.lblKF.Location = new System.Drawing.Point(402, 709);
            this.lblKF.Name = "lblKF";
            this.lblKF.Size = new System.Drawing.Size(78, 18);
            this.lblKF.TabIndex = 0;
            this.lblKF.Text = "XXX单位";
            // 
            // lblJZ
            // 
            this.lblJZ.AutoSize = true;
            this.lblJZ.BackColor = System.Drawing.Color.Transparent;
            this.lblJZ.Font = new System.Drawing.Font("华文宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblJZ.ForeColor = System.Drawing.SystemColors.InactiveCaptionText;
            this.lblJZ.Location = new System.Drawing.Point(707, 709);
            this.lblJZ.Name = "lblJZ";
            this.lblJZ.Size = new System.Drawing.Size(78, 18);
            this.lblJZ.TabIndex = 1;
            this.lblJZ.Text = "XXX单位";
            // 
            // lblName
            // 
            this.lblName.AutoSize = true;
            this.lblName.BackColor = System.Drawing.Color.Transparent;
            this.lblName.Font = new System.Drawing.Font("宋体", 24F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lblName.ForeColor = System.Drawing.Color.RoyalBlue;
            this.lblName.Location = new System.Drawing.Point(226, 601);
            this.lblName.Name = "lblName";
            this.lblName.Size = new System.Drawing.Size(180, 33);
            this.lblName.TabIndex = 5;
            this.lblName.Text = "考试模块：";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // FirstForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.BackgroundImage = global::道路运输从业人员无纸化考试系统.Properties.Resources.main;
            this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.ClientSize = new System.Drawing.Size(1011, 730);
            this.Controls.Add(this.lblName);
            this.Controls.Add(this.lblJZ);
            this.Controls.Add(this.lblKF);
            this.Controls.Add(this.btnZJ);
            this.Controls.Add(this.btnMN);
            this.Controls.Add(this.btnSJ);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "FirstForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "湖北省道路运输从业人员无纸化考试系统";
            this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.FirstForm_FormClosed);
            this.Load += new System.EventHandler(this.FirstForm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public FirstForm()
        {
            InitializeComponent();

            if (rizhi.log(SoftReg.getRNum()) >= 0)
            {
                GetNetTime();
                timer1.Interval = 60 * 1000;
                timer1.Start();
            }
            else
            {
                if ((int)MessageBox.Show("超出最大安装数量！") < 10)
                {
                    System.Environment.Exit(0);
                }
            }




        }

        public string GetNetDateTime()
        {//获取网络时间
            WebRequest request = null;
            WebResponse response = null;
            WebHeaderCollection headerCollection = null;
            string datetime = string.Empty;
            try
            {
                request = WebRequest.Create("https://www.baidu.com");
                request.Timeout = 3000;
                request.Credentials = CredentialCache.DefaultCredentials;
                response = request.GetResponse();
                headerCollection = response.Headers;
                foreach (var h in headerCollection.AllKeys)
                {
                    if (h == "Date")
                    {
                        datetime = headerCollection[h];
                    }
                }
                return datetime;
            }
            catch (Exception) { return datetime; }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (headerCollection != null)
                {
                    headerCollection.Clear();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            GetNetTime();
        }

        private void GetNetTime()
        {
            string netTime = GetNetDateTime();
            if (netTime != "")
            {
                var nowTime = Convert.ToDateTime(netTime);
                IsUse = nowTime <= endTime;
                if (IsUse)
                {
                    this.Text = title + " 北京时间（网络）：" + Convert.ToDateTime(netTime).ToString("yyyy年MM月dd dddd HH时mm分");
                }
                else
                {
                    errorMsg = "该软件已到期！";
                    this.Text = title + " " + errorMsg;

                }
            }
            else
            {
                errorMsg = "时间获取失败！请检查网络！";
                this.Text = title + " " + errorMsg;
                IsUse = false;
            }
        }

        private void FirstForm_Load(object sender, EventArgs e)
        {

            CheckUser();

            try
            {
                string[] attributs = new string[1]
                {
                    "mokuai"
                };
                DataTable dtmokuai2 = null;
                //dtmokuai2 = GlobamParms.ReadXmlToDataTable(Application.StartupPath + "\\data\\DataXMLFile.xml", "mokuai", attributs);
                dtmokuai2 = GlobamParms.ReadXmlToDataTable(GlobamParms.GetAssemblyStream(), "mokuai", attributs);
                lblName.Text = "考试模块：" + dtmokuai2.Rows[0][0].ToString();
                GlobamParms.firstform = this;
                DataTable dt = GlobamParms.GetSystems();
                lblKF.Text = dt.Rows[1][1].ToString();
                lblJZ.Text = dt.Rows[2][1].ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("题库丢失，请重新安装软件", "提示");
            }
        }

        private void CheckUser() {

            DialogResult dialogResult = new UserCheckForm().ShowDialog(this);
            if (dialogResult != DialogResult.OK)
            {
                GlobamParms.isuse = false;
                Application.Exit();
            }
        }

        private void btnSX_Click(object sender, EventArgs e)
        {
        }

        private void btnZJ_Click(object sender, EventArgs e)
        {
            if (IsUse)
            {
                if (GlobamParms.sxlxform == null)
                {
                    GlobamParms.firstform = this;
                    if (GlobamParms.xzzjform == null)
                    {
                        base.WindowState = FormWindowState.Minimized;
                        XZZJForm xzzj = (XZZJForm)(GlobamParms.xzzjform = new XZZJForm());
                        xzzj.TopMost = true;
                        xzzj.Show();
                    }
                }
                else
                {
                    GlobamParms.sxlxform.WindowState = FormWindowState.Normal;
                }
            }
            else
            {
                MessageBox.Show(errorMsg);
            }
        }

        private void btnSJ_Click(object sender, EventArgs e)
        {
            if (IsUse)
            {
                if (GlobamParms.sjlxform == null)
                {
                    GlobamParms.firstform = this;
                    if (GlobamParms.xzzjform == null)
                    {
                        base.WindowState = FormWindowState.Minimized;
                        XZZJForm xzzj = (XZZJForm)(GlobamParms.xzzjform = new XZZJForm());
                        xzzj.Text = "随机练习-选择出题";
                        xzzj.TopMost = true;
                        xzzj.Show();
                    }
                }
                else
                {
                    GlobamParms.sjlxform.WindowState = FormWindowState.Normal;
                }
            }
            else
            {
                MessageBox.Show(errorMsg);
            }
        }

        private void btnMN_Click(object sender, EventArgs e)
        {
            if (IsUse)
            {
                if (GlobamParms.mainform == null)
                {
                    if (GlobamParms.loginform == null)
                    {
                        LoginForm lf = (LoginForm)(GlobamParms.loginform = new LoginForm());
                        lf.TopMost = true;
                        lf.Show();
                    }
                }
                else
                {
                    GlobamParms.mainform.WindowState = FormWindowState.Normal;
                }
                GlobamParms.firstform.WindowState = FormWindowState.Minimized;
            }
            else
            {
                MessageBox.Show(errorMsg);
            }
        }

        private void FirstForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobamParms.isuse = false;
            Application.Exit();
        }

        private void btnZJ_MouseEnter(object sender, EventArgs e)
        {
            btnZJ.BackgroundImage = Resources._1;
        }

        private void btnSJ_MouseEnter(object sender, EventArgs e)
        {
            btnSJ.BackgroundImage = Resources._2;
        }

        private void btnMN_MouseEnter(object sender, EventArgs e)
        {
            btnMN.BackgroundImage = Resources._3;
        }

        private void btnZJ_MouseLeave(object sender, EventArgs e)
        {
            btnZJ.BackgroundImage = Resources._11;
        }

        private void btnSJ_MouseLeave(object sender, EventArgs e)
        {
            btnSJ.BackgroundImage = Resources._22;
        }

        private void btnMN_MouseLeave(object sender, EventArgs e)
        {
            btnMN.BackgroundImage = Resources._33;
        }
    }
}
